package com.iot.comprehensive.face.service.impl;

import com.iot.comprehensive.face.dao.AccountDao;
import com.iot.comprehensive.face.model.Account;
import com.iot.comprehensive.face.service.AccountService;
import com.iot.comprehensive.face.utils.SnowflakeIdWorker;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Author 朱泽玉
 * @Create 2021/6/26 13:48
 * @Description
 * @Modified By:
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    final AccountDao accountDao;

    public AccountServiceImpl(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public Account findByMobile(String mobile) {
        return accountDao.findByMobile(mobile);
    }

    @Override
    public Account findById(Long id) throws IOException {
        return accountDao.findById(id).orElse(null);
    }

    @Override
    public void update(Account new_account) {
        Account account = accountDao.findById(new_account.getId()).orElse(null);
        assert account != null;
        account.setMobile(new_account.getMobile());
        System.out.println("新密码："+new_account.getPassword());
        if (new_account.getPassword().equals("") ||DigestUtils.sha256Hex(new_account.getPassword()).equals(account.getPassword())){
            account.setPassword(DigestUtils.sha256Hex(new_account.getPassword()));
        }
        account.setIsCertified(new_account.getIsCertified());
//        account.set(new_account.getUser());
        accountDao.save(account);
    }


    @Override
    public void add(String mobile, String password) throws IOException {
        Account account1 = new Account();
        account1.setMobile(mobile);
        account1.setPassword(DigestUtils.sha256Hex(password));
        // 是否认证（初次进行注册的用户默认未进行认证）
        account1.setIsCertified(0);
        account1.setIdentity("user");
        // 设置唯一ID（根据雪花算法设置唯一的ID号作为主键）
        account1.setId(SnowflakeIdWorker.getUUID());
        accountDao.save(account1);
    }

    @Override
    public Account del(String mobile) {
        return null;
    }
}
